//
//  XQOrderListMenu.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/20.
//

#import "XQOrderListMenu.h"
#import "XQRadiusGradientView.h"
#import "NSString+DDAdd.h"

@interface XQOrderListMenu()
@property (nonatomic,strong) XQRadiusGradientView *gradientView;
@property (nonatomic,strong) NSMutableArray<UIButton *> *buttons;
@property (nonatomic, assign) double padding;
@end

@implementation XQOrderListMenu

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self makeUI];
        [self makeConstraints];
    }
    return self;
}

- (void)makeUI {
    [self addSubview:self.gradientView];
    
    NSArray *texts = @[@"All",@"In progress",@"Repayment",@"Finished"];
    
    double width  = 0;
    
    for (int i = 0; i < 4; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;
        [button setTitle:texts[i] forState:UIControlStateNormal];
        [button setTitleColor:i == 0 ? [UIColor whiteColor] : kMainTextColor forState:UIControlStateNormal];
        button.titleLabel.font = (i == 0 ? [UIFont boldSystemFontOfSize:18] : [UIFont systemFontOfSize:14]);
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttons addObject:button];
        [self.gradientView addSubview:button];
        width += [texts[i] widthForFont:[UIFont systemFontOfSize:15]];
    }
    
    self.padding = (([UIScreen mainScreen].width - 64) - width) / 3;
}

- (void)makeConstraints {
    [self.gradientView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@12);
        make.right.equalTo(@-12);
        make.height.mas_equalTo(50);
        make.bottom.equalTo(@0);
    }];
    
    for (int i = 0;i < self.buttons.count;i++) {
        UIButton *button = self.buttons[i];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            (void)make.centerY;
            if (i == 0) {
                make.width.mas_equalTo(62);
                make.left.equalTo(@0);
            }else {
                make.left.equalTo(self.buttons[i-1].mas_right);
                make.width.mas_equalTo(([UIScreen mainScreen].width - 24 - 62) / 3);
            }
        }];
    }
}

#pragma mark - setter

- (void)setIndex:(NSInteger)index {
    for (UIButton *btn in self.buttons) {
        [btn setTitleColor:kMainTextColor forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
    }
    [self.buttons[index] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.buttons[index].titleLabel.font = [UIFont boldSystemFontOfSize:18];
}

#pragma mark - private

- (void)buttonClick:(UIButton *)button{
    for (UIButton *btn in self.buttons) {
        [btn setTitleColor:kMainTextColor forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
    }
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    
    if (self.buttonHandle) {
        self.buttonHandle(button.tag);
    }
}

#pragma mark - lazy

- (XQRadiusGradientView *)gradientView {
    if (!_gradientView) {
        _gradientView = [[XQRadiusGradientView alloc] init];
        _gradientView.radius = 15;
        _gradientView.colors = @[(__bridge id)[UIColorFromRGB(0x49419E) CGColor],(__bridge id)[UIColorFromRGB(0x231E65) CGColor],(__bridge id)[UIColorFromRGB(0x231E65) CGColor]];
        _gradientView.startPoint = CGPointMake(0, 0.5);
        _gradientView.endPoint = CGPointMake(1, 0.5);
        _gradientView.locations = @[@(0),@(0.5),@(1)];
    }
    return _gradientView;
}

- (NSMutableArray<UIButton *> *)buttons {
    if (!_buttons) {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}

@end
